package com.csot.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.csot.model.CityIp;
import com.csot.model.TotalSegment;
import com.csot.ry.util.CacheKitUtil;
import com.csot.ry.util.UserUtils;
import com.csot.service.IndexService;
import com.jfinal.plugin.ehcache.CacheKit;
import com.csot.common.core.controller.BaseController;
import com.csot.common.core.domain.entity.SysUser;
import com.csot.common.utils.SecurityUtils;
import com.csot.common.utils.StringUtils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.BiMap;

/**
 * 空闲地址Controller
 *
 * @author LS
 * @date 2022-05-29
 */
@RestController
@RequestMapping("/ipmanagement/indexsegment")
public class IndexController extends BaseController {

    @Autowired
    private IndexService indexService;

    /**
     * 查询全量详细地址列表
     */
    @GetMapping("/show")
    public Map<String, Object> show() {

        Map<String, List<TotalSegment>> tMap = CacheKit.get("index-data", "total-segment");
        if (CollUtil.isEmpty(tMap)) {
            tMap = indexService.handleTotal();
        }
        Map<String, List<CityIp>> uMap = CacheKit.get("index-data", "use-segment");
        if (CollUtil.isEmpty(uMap)) {
            uMap = indexService.handleUse();
        }

        Map<String, List<CityIp>> oMap = CacheKit.get("index-data", "on-segment");
        if (CollUtil.isEmpty(oMap)) {
            oMap = indexService.handleUse_On();
        }

        Map<String, List<CityIp>> fMap = CacheKit.get("index-data", "off-segment");
        if (CollUtil.isEmpty(fMap)) {
            fMap = indexService.handleUse_Off();
        }

        Map<String, List<TotalSegment>> tCityMap = CacheKit.get("index-data", "city-total-segment");
        if (CollUtil.isEmpty(tCityMap)) {
            tCityMap = indexService.handleCityTotal();
        }
        Map<String, List<CityIp>> uCityMap = CacheKit.get("index-data", "city-use-segment");
        if (CollUtil.isEmpty(uCityMap)) {
            uCityMap = indexService.handleCityUse();
        }

        Map<String, Long> totalMap = indexService.cTotal(tMap, getCity());
        Map<String, Long> useMap = indexService.cUse_On_Off(uMap, getCity());
        Map<String, Long> idelMap = new HashMap<>();
        BiMap<String, String> map = CacheKit.get("dict-data", "ip_type");
        if (map != null) {
            map.forEach((k, v) -> {
                long total = 0L;
                if (CollUtil.isNotEmpty(totalMap) && StringUtils.isNotNull(totalMap.get(k))) {
                    total = totalMap.get(k);
                }
                long use = 0L;
                if (CollUtil.isNotEmpty(useMap) && StringUtils.isNotNull(useMap.get(k))) {
                    use = useMap.get(k);
                }
                long idel = total - use;
                idelMap.put(k, idel);
            });
        }

        Map<String, Long> use_On_Map = indexService.cUse_On_Off(oMap, getCity());
        Map<String, Long> use_Off_Map = indexService.cUse_On_Off(fMap, getCity());

        Map<String, Object> userMap = new HashMap<>();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptid = user.getDeptId();
        Long pdeptid = deptid;
        if (deptid != 100) {
            pdeptid = user.getDept().getParentId();
            userMap.put("city", CacheKitUtil.ip_dept.get(String.valueOf(pdeptid)));
        } else {
            userMap.put("city", "lns");
        }
        userMap.put("ip_type", UserUtils.getRole());


        Map<String, Object> resMap = new HashMap<>();
        resMap.put("show_total", totalMap);

        Long tcsl = Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs");
        useMap.put("ip_area", tcsl);

        resMap.put("show_use", useMap);
        resMap.put("show_idle", idelMap);


        resMap.put("show_use_on", use_On_Map);
        resMap.put("show_use_off", use_Off_Map);

        resMap.put("show_total_city", indexService.orderBy(indexService.cCityTotal(tCityMap)));

        Map<String, Long> tmap = indexService.orderBy(indexService.cCityUse(uCityMap));
        tmap.put("sy", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'sy'"));
        tmap.put("dl", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'dl'"));
        tmap.put("as", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'as'"));
        tmap.put("fs", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'fs'"));
        tmap.put("bx", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'bx'"));
        tmap.put("tl", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'tl'"));
        tmap.put("dd", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'dd'"));
        tmap.put("jz", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'jz'"));
        tmap.put("yk", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'yk'"));
        tmap.put("pj", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'pj'"));
        tmap.put("hld", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'hld'"));
        tmap.put("ly", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'ly'"));
        tmap.put("fx", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'fx'"));
        tmap.put("cy", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'cy'"));
        tmap.put("sbj", Db.queryLong("SELECT sum(tcs.ip_end_i-tcs.ip_start_i) as a from t_city_segment tcs where tcs.ip_city = 'sbj'"));
        resMap.put("show_use_city", tmap);


        resMap.put("user", userMap);

        return resMap;
    }

    private String getCity() {
        String city = "";
        SysUser user = SecurityUtils.getLoginUser().getUser();
        long d = user.getDeptId();
        if (d != 100) {
            Long deptid = SecurityUtils.getLoginUser().getUser().getDept().getParentId();
            city = CacheKitUtil.ip_dept.get(deptid + "");
        }
        return city;
        //return "";
    }


}
